package cn.avalon.user.controller;

import cn.avalon.basic.exception.BusinessException;
import cn.avalon.basic.utils.AjaxResult;
import cn.avalon.user.dto.LoginDto;
import cn.avalon.user.service.LogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @description: 登录处理接口
 * @author: Avalon
 * @date: 2022年08月27日 ： 12:08
 * @version: ver1.0
 * @email 709785494@qq.com
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LogininfoService logininfoService;

    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            // map中有两个信息：token - 做登录拦截， 登录信息对象 - 显示用户名(清空密码和盐值)
            Map<String,Object> map =  logininfoService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
            // 只要有业务异常都要加
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "登录失败");
        }
    }

    /**
     * 微信扫码登录
     * @param
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody LoginDto loginDto) {
        try {
            return logininfoService.wechatLogin(loginDto.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试");
        }
    }

    /**
     * 第一次微信扫码登录绑定手机号
     * @param map
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map) {
        try {
            return logininfoService.wechatBinder(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙，请稍后重试");
        }
    }


}
